(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.lookup.acctMoreDataController', acctMoreDataController);
	acctMoreDataController.$inject = [ '$scope', '$q', '$uibModalInstance', 'Constants', 'dialogsModule', 'gridService', 'modalService', 'layoutConfig', 'validatorConfig', 'authService' ];
	function acctMoreDataController($scope, $q, $uibModalInstance, Constants, dialogsModule, gridService, modalService, layoutConfig, validatorConfig, authService) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("acctMoreDataLookup");
		ctrl.dialogsModule = dialogsModule;
		ctrl.config = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		
		ctrl.ok = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['ok'] || dialogsModule.btnCallback['ok'](ctrl.account.selectedProperties) !== false) {
				$uibModalInstance.close();
			}
		};
		
		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['close'] || dialogsModule.btnCallback['close']() !== false) {
				$uibModalInstance.dismiss();
			}
		};
		
		ctrl.init = function() {
			ctrl.availableProperties = gridService.createInnerGridOptions($scope, ctrl.config.tableConfigs.table2, rowSelectionChanged2);
			ctrl.selectedProperties = gridService.createInnerGridOptions($scope, ctrl.config.tableConfigs.table1, rowSelectionChanged1);
			ctrl.availableProperties.data = 'vm.account.availableProperties';
			ctrl.selectedProperties.data = 'vm.account.selectedProperties';
			ctrl.availableProperties.multiSelect = true;
			ctrl.selectedProperties.multiSelect = true;
			ctrl.account = {};
			ctrl.search();
		};
		
		ctrl.addAcctMoreData = function(){
			var temp = ctrl.account.availableProperties;
			angular.forEach(ctrl.selectedRows2, function(selectedParam, key) {
				angular.forEach(temp, function(param, index) {
					if (param.parmId == selectedParam.parmId) {
						ctrl.account.availableProperties.splice(index, 1);
						pushArray(ctrl.account.selectedProperties, param);
					}
				});
			});
		};

		ctrl.removeAcctMoreData = function(){
			var temp = ctrl.account.selectedProperties;
			angular.forEach(ctrl.selectedRows1, function(selectedParam, key) {
				angular.forEach(temp, function(param, index) {
					if (param.parmId == selectedParam.parmId) {
						ctrl.account.selectedProperties.splice(index, 1);
						pushArray(ctrl.account.availableProperties, param);
					}
				});
			});
		};
		
		function pushArray(array1, value){
			if(array1.length == 0){
				array1.push(value);
				return;
			}else if(array1[array1.length - 1].parmDispOrder < value.parmDispOrder){
				array1.push(value);
				return;
			}
			var temp = array1;
			var returnFlag = true;
			angular.forEach(temp, function(array, key){
				if(returnFlag){
					if(array.parmDispOrder > value.parmDispOrder){
						array1.splice(key, 0, value);
						returnFlag = false;
					}
				}
			});
		}

		ctrl.search = function() {
			if(ctrl.dialogsModule.params && ctrl.dialogsModule.params.moreData){
				ctrl.account.moreData = ctrl.dialogsModule.params.moreData;
			}
			authService.search(ctrl.account).then(function(response) {
				angular.merge(ctrl.account, response.data);
				gridService.updateGridOptions(ctrl.account, ctrl.availableProperties);
			}, angular.noop);
		};

		function rowSelectionChanged1(eventRow, selectedRows) {
			ctrl.selectedRows1 = selectedRows;
		}
		
		function rowSelectionChanged2(eventRow, selectedRows) {
			ctrl.selectedRows2 = selectedRows;
		}

	}
})(angular);
